// pages/options/options.js
var app = getApp();
let util=require("../../utils/util.js");
var x, y, x1, y1, x2, y2, index, currindex, n, yy;
var arr1 = [
];
Page({
  
  /**
   * 页面的初始数据
   */
  data: {
    
    mainx: 0,
    
    start: { x: 0, y: 0 },
    myoptionlist: [{
      id: 10,
      optionname: "推荐"
    }, {
      id: 11,
      optionname: "健身0"
      },{
        id: 12,
        optionname: "美容护肤0"
      }, {
        id: 13,
        optionname: "健身1"
      }, {
        id: 14,
        optionname: "营养保健"
      },
      {
        id: 15,
        optionname: "推荐"
      }, {
        id: 16,
        optionname: "健身"
      }, 
    ],
    otheroptionlist: [{
      id: 0,
      name: "推荐"
    }, {
      id: 1,
      name: "健身"
    }, {
        id: 2,
        name: "美容护肤"
    }, {
      id: 3,
      name: "健身"
    }, {
      id: 4,
      name: "营养保健"
    },
    {
      id: 5,
      name: "推荐"
    }, {
      id: 6,
      name: "健身"
    }, {
      id: 7,
      name: "美容护肤"
    }, 
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //获取屏幕宽高
    var that = this;
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          screenHeight: res.windowHeight,
          screenWidth: res.windowWidth,
        });
      }
    });

    //请求后台获取数据
    //获取我的频道
    wx.request({
      url: util.pindao_my,
      data: {
        openid: app.globalData.openid
      },
      success: function (res) {
        console.log("---------wodepindao---------");
        console.log(res);
        if (res.data.uclass){
          var wodepindao = res.data.uclass;
          arr1=res.data.uclass;
          that.setData({
            myoptionlist: wodepindao
          })
        }
      },
      fail: function (err) {
        console.log("err" + err);
      }
    })
    //获取其他频道
    wx.request({
      url: util.pindao_qita,
      data:{
        openid:app.globalData.openid
      },
      success:function(res){
        console.log("---------qitapindao---------");
        console.log(res);
        var qitapindao=res.data.class;
        console.log(typeof(qitapindao));
        that.setData({
          otheroptionlist:qitapindao
        })
      },
      fail:function(err){
        console.log("err"+err);
      }
    })
     
  },

  //关闭当前页
  closePage:function(){
    console.log("close");
    wx.switchTab({
      url: '/pages/index/index',
    })
   
  },

  //删除已选频道
  removeOption:function(e){
   var that=this;
    var classid = e.currentTarget.dataset.id;
    console.log(classid);
    wx.showModal({
      title: '',
      content: '您确认删除此频道',
      confirmColor:"#ff1313",
      success:function(res){
        if (res.confirm) {
          console.log('用户点击确定')
          //调用接口，将当前选项id传后台，重新刷新页面
          wx.request({
            url: util.del_pindao,
            data:{
              openid:app.globalData.openid,
              classid: classid
            },
            success:function(res){
              console.log("删除成功")
              console.log(res)
            },
            fail:function(err){
              console.log("删除失败"+err)
            }
          })
          //刷新页面，重新请求我的频道和其他频道
          that.onLoad();
        } else if (res.cancel) {
          console.log('用户点击取消')
        }
         
        
      }
    })
    
    

  },

  //添加频道
  addOption:function(e){
    var that=this;
    var classid=e.currentTarget.dataset.id;
    console.log("用户添加频道，请求后台，刷新页面");
    console.log(classid);
    console.log(app.globalData.openid);
    //添加接口
    wx.request({
      url: util.add_pindao,
      data:{
        classid:classid,
        openid: app.globalData.openid
      },
      success:function(res){
        console.log(res)
         //刷新页面，重新请求我的频道和其他频道
        that.onLoad();
      },
      fail:function(err){
        console.log("err"+err)
      }
    })
   
    
  },

  //拖拽改变频道的顺序
  movestart: function (e) {
    currindex = e.target.dataset.index;
    x = e.touches[0].clientX;
    y = e.touches[0].clientY;
    x1 = e.currentTarget.offsetLeft;
    y1 = e.currentTarget.offsetTop;
  },
  move: function (e) {
    yy = e.currentTarget.offsetTop;
    x2 = e.touches[0].clientX - x + x1;
    y2 = e.touches[0].clientY - y + y1;
    this.setData({
      mainx: currindex,
      opacity: 0.7,
      start: { x: x2, y: y2 }
    })
  },
  moveend: function () {
    var that=this;
    if (y2 != 0) {
      var arr = [];
      for (var i = 0; i < that.data.myoptionlist.length; i++) {
        arr.push(that.data.myoptionlist[i]);
        console.log("******arr[i]*******")
        console.log(arr[i])
      }
     
      var nx = that.data.myoptionlist.length;
      n = 1;
      for (var k = 2; k < nx; k++) {
        if (y2 > (52 * (k - 1) + k * 2 - 26)) {
          n = k;
        }
      }
      if (y2 > (52 * (nx - 1) + nx * 2 - 26)) {
        n = nx;
      }
      console.log("------------")
      arr = arr1;
      console.log(arr);
      console.log(arr1)
     
      arr.splice((currindex - 1), 1);
      arr.splice((n - 1), 0, arr1[currindex - 1]);
      arr1 = [];
      for (var m = 1; m < that.data.myoptionlist.length; m++) {
        console.log("arr[m]"+arr[m]);
        arr[m].id = m + 1;
        arr1.push(arr[m]);
      }
     
      this.setData({
        mainx: "",
        myoptionlist: arr,
        opacity: 1
      })
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})